<template>
  <div>
    <div>
      <el-form ref="form" :model="sysUser" :rules="rules" label-width="100px" size="small">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="所在部门" prop="deptId">
              <treeselect
                v-model="sysUser.deptId"
                :options="deptList"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择所在部门"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="账号" prop="userName">
              <el-input v-model="sysUser.userName" placeholder="请输入账号" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="sysUser.nickName" placeholder="请输入昵称" clearable/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="sysUser.email" placeholder="请输入邮箱" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="sysUser.phone" placeholder="请输入手机号" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="sysUser.sex">
                <el-radio v-for="status in sexOptions" :key="status.dictValue" :label="status.dictValue">
                  {{ status.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="sysUser.status">
                <el-radio v-for="status in statusOptions" :key="status.dictValue" :label="status.dictValue">
                  {{ status.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="直属领导" prop="leaderName">
              <el-input v-model="sysUser.leaderName" placeholder="请输入直属领导" disabled style="width: 80%" clearable/>
              <el-button type="primary" style="width: 15%;margin-left: 5px" @click="selectLeader">选择</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="text-align: center">
          <el-button type="primary" @click="add">添加</el-button>
          <el-button type="warning" @click="sysUser = {sex:0,status:0}">重置</el-button>
        </el-form-item>
      </el-form>
      <!--选择用户的组件-->
      <select-system-user ref="selectUser" v-if="showSelectUserDialog" @select="confirmSelectUser"></select-system-user>
    </div>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import selectSystemUser from '@/views/components/selectSystemUser/index'
import sysDeptApi from "@/api/system/sysDept";
import sysUserApi from "@/api/system/sysUser";
export default {
  name: "sys-user-add",
  components:{
    Treeselect,
    selectSystemUser
  },
  data(){
    return{
      sysUser: {
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        leaderId: undefined,
        leaderName: undefined,
        email: undefined,
        phone: undefined,
        sex: 0,
        status: 0,
      },
      //部门的下接树的数据
      deptList: [],
      //验证规则
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, message: '用户名最短2位', trigger: 'change'}
        ],
        nickName: [
          {required: true, message: '请输入昵称', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          { min: 11, max: 11, message: '手机号必须是11个字符', trigger: 'blur' }
        ],
        deptId: [
          {required: true, message: '请选择部门', trigger: 'blur'}
        ]
      },
      //状态字典数据
      statusOptions: [],
      //性别的字典数据
      sexOptions: [],
      //是否打开选择用户的弹出层组件
      showSelectUserDialog:false
    }
  },
  created() {
    sysDeptApi.listDept().then(res=>{
      this.deptList=res.data
    })
    //加载状态的字典数据
    this.getDataByType("sys_status_type").then(res => {
      this.statusOptions = res.data;
    })
    this.getDataByType("sys_user_sex").then(res => {
      this.sexOptions = res.data;
    })
  },
  methods:{
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    //添加
    add(){
      //触发表单验证
      this.$refs["form"].validate(valid=>{
        if(valid){
          sysUserApi.save(this.sysUser).then(res=>{
            this.$message.success(res.msg)
            this.$emit("after")
            this.$emit("close")
          })
        }
      })
    },
    //选择直属领导
    selectLeader(){
      this.showSelectUserDialog=true;
      this.$nextTick(_ => {
        this.$refs.selectUser.initSelectUser();
      })
    },
    //选择用户的弹出层触发之后确定之后回调的方法
    confirmSelectUser(user){
      this.sysUser.leaderName=user.nickName;
      this.sysUser.leaderId=user.userId
    }
  }
}
</script>

<style scoped>

</style>
